package com.example.cslp.mapper;


import com.example.cslp.pojo.entity.Course;
import com.example.cslp.pojo.entity.Record;
import com.example.cslp.pojo.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author zcy
 */
@Mapper
public interface RecordMapper {
    //订阅课程
    @Insert("INSERT INTO record (course_id, user_id) VALUES (#{courseId}, #{userId})")
    Boolean addRecord(Record record);


    //订阅课程扣分
    @Update("UPDATE users SET points = points - " +
            "(SELECT price FROM courses WHERE id = #{courseId})*(SELECT discount FROM courses WHERE id = #{courseId})" +
            " WHERE id = #{userId}")
    Boolean payPoints(@Param("record") Record record);

    //删除课程
    @Delete("DELETE FROM record WHERE user_id = #{u_id} AND course_id = #{c_id}")
    Boolean delRecord(@Param("u_id") String u_id,@Param("c_id") String c_id);
//得到订阅列表
    @Select("SELECT * FROM courses WHERE id IN (SELECT course_id FROM record WHERE user_id = #{name})")
    List<Course> getRecordByUser(String name);
//得到未订阅列表
    @Select("SELECT * FROM courses WHERE id NOT IN (SELECT course_id FROM record WHERE user_id = #{name})")
    List<Course> getUnsubscribedCoursesByUser(String name);


}



